package com.mission.schedule.activity;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.RepeatBean;
import com.mission.schedule.clock.RepeatDateUtil;
import com.mission.schedule.clock.WriteAlarmClock;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.db.DBSourse;
import com.mission.schedule.utils.ActivityManager1;
import com.mission.schedule.utils.AfterPermissionGranted;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.EasyPermissions;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.RepeatDateUtils;
import com.mission.schedule.utils.SharedPrefUtil;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by liwei on 2017/2/27.
 */

public class NewLoginActivity extends Activity implements View.OnClickListener ,EasyPermissions.PermissionCallbacks {

    private CardView card1;
    private CardView card2;

    SharedPrefUtil sharedPrefUtil;

    ActivityManager1 activityManager;

    String user = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_login);
        instance = this;

        sharedPrefUtil = new SharedPrefUtil(this, ShareFile.USERFILE);
        activityManager = ActivityManager1.getInstance();
        activityManager.addActivities(this);
        toWriteData(DBSourse.dataBaseName, R.raw.data);
        user = getIntent().getStringExtra("user");
        sharedPrefUtil.putString(this, ShareFile.USERFILE, ShareFile.CHANGJIANDIANZAN,"");
        sharedPrefUtil.putString(this, ShareFile.USERFILE, ShareFile.ADDQDCOPY, "0");
        sharedPrefUtil.putString(this, ShareFile.USERFILE, ShareFile.USERID, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.WEEKCOLOR1, "4");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.MONTHCOLOR2, "3");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.YEARCOLOR3, "2");
        checkPhonePermission();
        if (user != null && !user.isEmpty()){
            MacAddressExit();
        }
        WriteAlarmClock.clearAlarmClock(getApplicationContext());
        Intent intentappwidget = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        Bundle extras = new Bundle();
        intentappwidget.putExtras(extras);
        sendBroadcast(intentappwidget);
        card1 = (CardView) findViewById(R.id.card1);
        card2 = (CardView) findViewById(R.id.card2);
        card1.setOnClickListener(this);
        card2.setOnClickListener(this);

        String pushAlias = sharedPrefUtil.getString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.PUSH_ALIAS, "0");// 第一次
        if ("0".equals(pushAlias)) {
            // 调用JPush API设置Alias
            String udid = JPushInterface.getRegistrationID(getApplicationContext());
            JPushInterface.setAliasAndTags(getApplicationContext(),udid, null, null);
            sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.PUSH_ALIAS, "1");
        }

    }


    public static NewLoginActivity instance = null;


    private void MacAddressExit() {
        final AlertDialog builder = new AlertDialog.Builder(this).create();
        builder.setCancelable(false);
        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        window.setAttributes(params);// 设置生效
        window.setContentView(R.layout.dialog_alert_ok);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);

        delete_tv.setText("检测到当前账号在其他终端进行登录,如果不是本人登录，请重新登录!");
        delete_ok.setText("退出");
        delete_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clear();
                builder.dismiss();

            }
        });
    }

    private synchronized void clear() {
        WriteAlarmClock.clearAlarmClock(getApplicationContext());
        if (!"".equals(sharedPrefUtil.getString(NewLoginActivity.this,
                ShareFile.USERFILE, ShareFile.TELEPHONE, ""))) {
            sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                    ShareFile.TELEPHONE, "");
        } else {
            sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                    ShareFile.USEREMAIL, "");
        }
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.ZDTX, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.BDTX, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.ISTX, "1");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.QIANDAOSTRING, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.ISADMIN, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.USERID, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.USERNAME, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.ISNEWYOUKE, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.USERBACKGROUNDPATH, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.USERPHOTOPATH, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.USERSTATE, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.FIRSTLOGIN, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.U_ACC_NO, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.MAXFOCUSID, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.OUTWEEKFAG, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.UPDATESETTIME, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.UPDATENOTESETTIME, "2017-01-01%2B00:00:00");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.DOWNSCHTIME, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.DOWNREPTIME, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.RINGSTATE, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.ISYOUKE, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.DOWNTAGDATE, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.OPENSTYLESTATE, "2");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNSCHTIME, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNRepTIME, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNOldTIME, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.PUSH_ALIAS, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.REFRESHFRIEND, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.FIRSTDOWNFOCUSSCH, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.SHOUCANGDATA, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.DOWNNEWFOCUSSHARESCHDATE, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.DOWNNEWFOCUSSHAREREPDATE, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.KuaiJieSouSuo, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.SHAREDATA, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.LOCALPATH, "1");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.ISMOSTIMAGE, "1");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.ISSJMOSTIMAGE, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE1, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE2, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE3, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE4, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE5, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE6, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE7, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.DQRBEFORETIME, "4320");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.ZJF, "0");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.UNTASKEND, "1");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.PERSONDOWNTIME, "2016-01-01%2B00:00:00");
        sharedPrefUtil.putString(
                NewLoginActivity.this,
                ShareFile.USERFILE,
                ShareFile.PERSONDOWNTIME,
                "2016-01-01");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.WELCOMETOLOGIN, "1");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.WEEKCOLOR1, "4");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.MONTHCOLOR2, "3");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.YEARCOLOR3, "2");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.DOWNTASKTIME, "");
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE,
                ShareFile.DOWNRepeatTASKTIME, "");
        ClearMAC();
        App.getDBcApplication().douleClear();
        sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.PERSONDOWNTIME, "2016-01-01");
        Intent intentappwidget = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        Bundle extras = new Bundle();
        intentappwidget.putExtras(extras);
        sendBroadcast(intentappwidget);
    }
    private void ClearMAC() {
        if (NetUtil.getConnectState(NewLoginActivity.this) != NetUtil.NetWorkState.NONE) {
            if (sharedPrefUtil.getString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                final String path = URLConstants.修改MAC地址 + sharedPrefUtil.getString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.USERID, "") + "&uClintAddr="
                        + "" + "&uTocode=android";
                StringRequest request = new StringRequest(Method.GET, path,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String s) {

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                    }
                });
                request.setTag("down");
                request.setRetryPolicy(new DefaultRetryPolicy(20000, 1, 1.0f));
                App.getHttpQueues().add(request);
            }
        } else {
            return;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.card1:
                sharedPrefUtil.putString(this, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "-1");//不是新游客
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.DQRBEFORETIME, "4320");
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.ZJF, "0");
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.UNTASKEND, "1");
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.PERSONDOWNTIME, "2016-01-01%2B00:00:00");
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.PERSONDOWNTIME, "2016-01-01");
                Intent newt = new Intent(this, PhoneCodeLoginNewActivity.class);
                newt.putExtra("type", 0);
                newt.putExtra("login", false);
                newt.putExtra("welcome", true);
                if (user != null) {
                    newt.putExtra("user", user);
                }
                startActivity(newt);
                break;
            case R.id.card2:
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 1, "", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("18:12",1 ,"", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 3 ,"20日", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 3 ,"10日", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("18:12", 3 ,"10日", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("18:12", 3 ,"31日", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("18:12", 3 ,"30日", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 2, "星期三", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("16:12", 2, "星期三", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 2, "星期二", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 2, "星期四", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("18:12", 5, "", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("18:12", 6, "二月廿六", 1);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 6, "二月廿六", 1);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 4, "04-11", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("09:12", 4, "04-11", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 4, "05-11", 0);
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 4, "02-29", 0);
//                List<String> list = RepeatDateUtil.getSevenWorkTime();
//                Log.e("TAG", list.size()+"");
//                RepeatDateUtil.dateTimeToChildAlarmDate("10:12", 5, "", 0);
                toWriteData(DBSourse.dataBaseName, R.raw.data);
                sharedPrefUtil.putString(this, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0");//是新游客
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.DQRBEFORETIME, "4320");
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.ZJF, "0");
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.UNTASKEND, "1");
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.PERSONDOWNTIME, "2016-01-01%2B00:00:00");
                sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.PERSONDOWNTIME, "2016-01-01");
                sharedPrefUtil.putString(this, ShareFile.USERFILE, ShareFile.USERSTATE, "1");
                new WriteDataBase2().execute();
                break;
        }
    }

    public static int getRandNum(int min, int max) {
        int randNum = min + (int)(Math.random() * ((max - min) + 1));
        return randNum;
    }
    private void ResginAsync(String path) {

//        if (NetUtil.getConnectState(this) == NetUtil.NetWorkState.NONE) {
//            alertFailIntenetDialog(0,"");
//        } else {
            int id = getRandNum(1,9999);
            sharedPrefUtil.putString(NewLoginActivity.this,
                    ShareFile.USERFILE,
                    ShareFile.USERID, -id + "");
            sharedPrefUtil.putString(
                    NewLoginActivity.this,
                    ShareFile.USERFILE,
                    ShareFile.USERNAME,
                    "游客12110" + id);

            sharedPrefUtil.putString(NewLoginActivity.this,
                    ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0");
            sharedPrefUtil.putString(NewLoginActivity.this,
                    ShareFile.USERFILE, ShareFile.USERSTATE, "1");
            sharedPrefUtil.putString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.LOGINTYPE, "1");
            sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.SHUNYANTYPE, "0");
            sharedPrefUtil.putString(NewLoginActivity.this, ShareFile.USERFILE, ShareFile.ISYOUKE, "2");
            App.getDBcApplication().douleClear();
            getJson();
            App.getDBcApplication().insertTagIntenetData(101, "生日", 0, "0", 0, 0, "1", "");
            String[] emptyTag = {"生活","工作","理财","其它","分类A","分类B"};
            for (int i = 0; i < emptyTag.length ; i++){
                App.getDBcApplication().insertTagIntenetData(-(i + 1), emptyTag[i], i + 1, "0", 1, 1, ""+(i + 2), "");
            }

            sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.LOGINTYPE, "1");
            Intent intent = new Intent(NewLoginActivity.this, MainActivity.class);
            intent.putExtra("isyouke", true);
            startActivity(intent);
            finish();
            activityManager.doAllActivityFinish();
//        }
    }

    private void alertFailIntenetDialog(int type, String msg) {
        final AlertDialog builder = new AlertDialog.Builder(this).create();
        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);// 设置生效
        window.setGravity(Gravity.CENTER);
        window.setContentView(R.layout.dialog_alert_ok);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);
        if (type == -1) {
            delete_tv.setText(msg);
        } else if (type == 0) {
            delete_tv.setText("网络请求超时！");
        } else {
            delete_tv.setText(msg);
        }

        delete_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                builder.cancel();
            }
        });

    }
    private static final int RC_LOCATION_CONTACTS_PERM = 124;
    @AfterPermissionGranted(RC_LOCATION_CONTACTS_PERM)
    private void checkPhonePermission() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Have permissions, do the thing!
        } else {
            // Ask for both permissions
            EasyPermissions.requestPermissions(this, "该应用需要这些权限，为了保证应用正常运行!",
                    RC_LOCATION_CONTACTS_PERM, perms);
        }
        sharedPrefUtil.putString(this, ShareFile.USERFILE, ShareFile.PERMISSIONSTATE, "1");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // EasyPermissions handles the request result.
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Log.d("TAG", "onPermissionsGranted:" + requestCode + ":" + perms.size());
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
// (Optional) Check whether the user denied permissions and checked NEVER ASK AGAIN.
        // This will display a dialog directing them to enable the permission in app settings.
        EasyPermissions.checkDeniedPermissionsNeverAskAgain(this,
                getString(R.string.rationale_ask_again),
                R.string.action_settings, R.string.cancel, null, perms);
    }

    class WriteDataBase2 extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            String path = URLConstants.新游客试用 + "?phoneType="+android.os.Build.MODEL.replace(" ","")+
                    "&version="+getVersion()+"&pushMac="+JPushInterface.getRegistrationID(NewLoginActivity.this);
            ResginAsync(path);
        }

    }
    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return "V" + version;
        } catch (Exception e) {
            e.printStackTrace();
            return "V6.3.2";
        }
    }

    private void getJson(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder sb = new StringBuilder();
                try{
                    AssetManager assetManager = NewLoginActivity.this.getAssets();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(assetManager.open("cytx.json")));
                    String line;
                    while ((line = bufferedReader.readLine()) != null){
                        sb.append(line);
                    }
                    sharedPrefUtil.putString(NewLoginActivity.this,ShareFile.USERFILE,
                            ShareFile.CYTXJSON, sb.toString());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }



    /**
     * 初始化数据库
     *
     * @param dataBaseName
     * @param resourse
     */
    private void toWriteData(String dataBaseName, int resourse) {
        InputStream is = getResources().openRawResource(resourse); // 欲导入的数据库
        OutputStream fos;
        try {
            String dbpath = "/data/data/com.mission.schedule/databases/" + dataBaseName;
            if (DBSourse.createFile(dbpath)) {
                fos = new FileOutputStream(dbpath);
                byte[] buffer = new byte[1024];
                int count = 0;
                while ((count = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, count);
                }
                fos.flush();
                fos.close();
                is.close();

                SharedPreferences sp = getSharedPreferences("localchedule", Context.MODE_PRIVATE);
                SharedPreferences.Editor e = sp.edit();
                e.putString("isDataWrite", "1");// 0为写入数据库文件
                e.commit();
                Log.d("TAG", "数据库导入成功");
            } else {
                Log.d("TAG", "数据库导入失败");
            }
        } catch (Exception e) {
            Log.d("TAG", "数据库导入失败");
        }
    }


    class Beans{

        /**
         * list : [{"createTime":"2017-02-15T10:36:33","id":2,"integrals":0,"phoneType":"ios","remark":null,"remark1":null,"versions":0}]
         * message : 游客登陆成功
         * status : 0
         */

        public String message;
        public int status;
        public List<ListBean> list;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public class ListBean {
            /**
             * createTime : 2017-02-15T10:36:33
             * id : 2
             * integrals : 0
             * phoneType : ios
             * remark : null
             * remark1 : null
             * versions : 0
             */

            public String createTime;
            public int id;
            public int integrals;
            public String phoneType;
            public Object remark;
            public Object remark1;
            public int versions;

            public String getCreateTime() {
                return createTime;
            }

            public void setCreateTime(String createTime) {
                this.createTime = createTime;
            }

            public int getId() {
                return id;
            }

            public void setId(int id) {
                this.id = id;
            }

            public int getIntegrals() {
                return integrals;
            }

            public void setIntegrals(int integrals) {
                this.integrals = integrals;
            }

            public String getPhoneType() {
                return phoneType;
            }

            public void setPhoneType(String phoneType) {
                this.phoneType = phoneType;
            }

            public Object getRemark() {
                return remark;
            }

            public void setRemark(Object remark) {
                this.remark = remark;
            }

            public Object getRemark1() {
                return remark1;
            }

            public void setRemark1(Object remark1) {
                this.remark1 = remark1;
            }

            public int getVersions() {
                return versions;
            }

            public void setVersions(int versions) {
                this.versions = versions;
            }
        }
    }
}
